#include "dialogcreate.h"
#include "ui_dialogcreate.h"
#include "dbmgr.h"

#include <QFile>
#include <QDir>
#include <QRegExp>
#include <QRegExpValidator>
#include <QFileDialog>
#include <QDebug>
#include <QSpacerItem>

#include <iostream>

using namespace std;

DialogCreate::DialogCreate(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogCreate),
    m_isValid(false)
{
    ui->setupUi(this);
    QRegExp re("[-_0-9a-zA-Z]*");
    ui->editProjectName->setValidator(new QRegExpValidator(re, this));
    QRegExp suffixRe("[ 0-9a-zA-Z]*");
    ui->editSuffix->setValidator(new QRegExpValidator(suffixRe, this));

    m_vSourceSuffixes = {"c", "cpp", "cc", "cxx", "C"};
    m_vHeaderSuffixes = {"h", "hpp", "hxx"};

    addComboBox();
}

DialogCreate::~DialogCreate()
{
    delete ui;
}

void DialogCreate::on_buttonOK_clicked()
{
    QString projectName = ui->editProjectName->text();
    QString sourcePath = ui->editSourcePath->text();

    if (!isNameAndPathValid())
    {
        return;
    }

    shared_ptr<set<string>> suffixes = getSuffixes();
    QString path = QFileDialog::getExistingDirectory(this, tr("Save project"),
                                                    "/",
                                                    QFileDialog::ShowDirsOnly
                                                    | QFileDialog::DontResolveSymlinks);
    if (QFile::exists(path))
    {
        QDir dir(path);
        if (dir.mkdir(projectName))
        {
            QString projectPath = dir.absoluteFilePath(projectName);
            m_isValid = true;
            m_projectName = projectName.toStdString();
            m_projectPath = projectPath.toStdString();
            m_sourcePath = sourcePath.toStdString();
            m_suffixes = suffixes;
            close();
        }
        else
        {
            ui->labelWarnName->setText("Can not create project folder(folder exists?)");
        }
    }
    else
    {
        ui->labelWarnName->setText("Project path doesn't exist");
    }
}

void DialogCreate::on_buttonOpen_clicked()
{
    QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                                      "/",
                                                      QFileDialog::ShowDirsOnly
                                                      | QFileDialog::DontResolveSymlinks);
    ui->editSourcePath->setText(dir);
}

bool DialogCreate::isNameAndPathValid()
{
    QString projectName = ui->editProjectName->text();
    QString sourcePath = ui->editSourcePath->text();

    bool result = true;

    if (projectName.isEmpty())
    {
        ui->labelWarnName->setText("Project name can not be empty");
        result = false;
    }
    else
    {
        ui->labelWarnName->setText("");
        result = true;
    }
    if (sourcePath.isEmpty())
    {
        ui->labelWarnPath->setText("Source path can not be empty");
        result = false;
    }
    else if (!QFile::exists(sourcePath))
    {
        ui->labelWarnPath->setText("Source path doesn't exist");
        result = false;
    }
    else
    {
        ui->labelWarnPath->setText("");
        result = true;
    }

    return result;
}

std::shared_ptr<std::set<string> > DialogCreate::getSuffixes()
{
    shared_ptr<set<string>> result = make_shared<set<string>>();
    for (QCheckBox *cb : m_vCheckBox)
    {
        if (cb->isChecked())
        {
            result->insert(cb->text().toStdString());
        }
    }
    QStringList sl = ui->editSuffix->text().split(" ");
    int size = sl.size();
    for (int i = 0; i < size; i++)
    {
        if (!sl.at(i).isEmpty())
        {
            result->insert(sl.at(i).toStdString());
        }
    }
    return result;
}

void DialogCreate::addComboBox()
{
    for (string s : m_vSourceSuffixes)
    {
        QCheckBox *cb = new QCheckBox(QString::fromStdString(s), this);
        cb->setChecked(true);
        ui->layoutSource->addWidget(cb);
        m_vCheckBox.push_back(cb);
    }
    ui->layoutSource->addSpacerItem(new QSpacerItem(40, 10, QSizePolicy::Expanding, QSizePolicy::Minimum));

    for (string s : m_vHeaderSuffixes)
    {
        QCheckBox *cb = new QCheckBox(QString::fromStdString(s), this);
        cb->setChecked(true);
        ui->layoutHeader->addWidget(cb);
        m_vCheckBox.push_back(cb);
    }
    ui->layoutHeader->addSpacerItem(new QSpacerItem(40, 10, QSizePolicy::Expanding, QSizePolicy::Minimum));
}

std::string DialogCreate::sourcePath() const
{
    return m_sourcePath;
}

bool DialogCreate::isValid() const
{
    return m_isValid;
}

std::shared_ptr<std::set<std::string> > DialogCreate::supportedSuffixes() const
{
    return m_suffixes;
}

std::string DialogCreate::projectName() const
{
    return m_projectName;
}

std::string DialogCreate::projectPath() const
{
    return m_projectPath;
}
